package com.tim.app.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Advertisement;
import com.tim.app.server.entry.Answer;
import com.tim.app.server.entry.Article;
import com.tim.app.server.entry.Dynamic;
import com.tim.app.server.entry.Problem;
import com.tim.app.server.entry.SelectInfo;
import com.tim.app.server.entry.Story;
import com.tim.app.server.entry.Topic;
import com.tim.app.service.MusicService;
import com.tim.app.ui.activity.DynamicDetailActivity;
import com.tim.app.ui.activity.GeneralWebActivity;
import com.tim.app.ui.activity.MainActivity;
import com.tim.app.ui.activity.OfficialNoticeActivity;
import com.tim.app.ui.activity.StoryDetailActivity;
import com.tim.app.ui.activity.selected.AnswerDetailActivity;
import com.tim.app.ui.activity.selected.ArticleDetailActivity;
import com.tim.app.ui.activity.selected.QuestionAndAnswerDetailActivity;
import com.tim.app.ui.activity.selected.TopicDetailActivity;
import com.tim.app.ui.activity.setting.PhoneLoginActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ViewGT {


    /**
     * 跳转webview页面
     */
    public static void gotoGeneralWebActivity(Context context, String url, boolean isFromPush) {
        Intent intent = new Intent(context, GeneralWebActivity.class);
        intent.putExtra(AppKey.WEB_URL, url);
        intent.putExtra(AppKey.IS_FROM_PUSH, isFromPush);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 跳转到主页
     *
     * @param context
     */
    public static void gotoMainActivity(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 跳转webview页面
     */
    public static void gotoGeneralWebActivityWithTitle(Context context, String url, boolean isFromPush, String title) {
        Intent intent = new Intent(context, GeneralWebActivity.class);
        intent.putExtra(AppKey.WEB_URL, url);
        intent.putExtra(AppKey.WEB_TITLE, title);
        intent.putExtra(AppKey.IS_FROM_PUSH, isFromPush);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 跳转登陆页面
     */
    public static void gotoLoginActivity(Context context) {
        Intent intent = new Intent(context, PhoneLoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    /**
     * 跳转登陆页面
     */
    public static void gotoLoginActivityCanBack(Context context) {
        Intent intent = new Intent(context, PhoneLoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("canBack", true);
        context.startActivity(intent);
    }

    /**
     * 处理广告的跳转
     *
     * @param context
     */
    public static void processAdvertisement(Context context, Advertisement advertisement) {
        if (null != advertisement) {
            if (Advertisement.TYPE_IN == advertisement.getInside()) {
                gotoAdverisementDetail(context, advertisement, false);
            } else if (Advertisement.TYPE_OUT == advertisement.getInside()) {
                gotoAdverisementDetail(context, advertisement, true);
            } else {
                if (!TextUtils.isEmpty(advertisement.getUrl())) {
                    gotoGeneralWebActivity(context, advertisement.getUrl(), true);
                }
            }
        }
    }

    private static void gotoAdverisementDetail(Context context, Advertisement advertisement, boolean isFromPush) {
        if (AppKey.TYPE_ANSWER.equals(advertisement.getContentType())) {
            Answer answer = new Answer();
            answer.setId(advertisement.getUrl());
            AnswerDetailActivity.start(context, answer, isFromPush);
        } else if (AppKey.TYPE_ARTICLE.equals(advertisement.getContentType())) {
            Article article = new Article();
            article.setId(advertisement.getUrl());
            ArticleDetailActivity.start(context, article, isFromPush);
        } else if (AppKey.TYPE_STORY.equals(advertisement.getContentType())) {
            Story story = new Story();
            story.setId(advertisement.getUrl());
            ArrayList<Story> storyArrayList = new ArrayList<>();
            storyArrayList.add(story);
            StoryDetailActivity.start(context, storyArrayList, 0, isFromPush);
        } else if (AppKey.TYPE_DYNAMIC.equals(advertisement.getContentType())) {
            Dynamic dynamic = new Dynamic();
            dynamic.setId(advertisement.getUrl());
            DynamicDetailActivity.start(context, dynamic, isFromPush);
        } else if (AppKey.TYPE_TOPIC.equals(advertisement.getContentType())) {
            Topic topic = new Topic();
            topic.setId(advertisement.getUrl());
            TopicDetailActivity.start(context, topic, false);
        } else if (AppKey.TYPE_PROBLEM.equals(advertisement.getContentType())) {
            Problem problem = new Problem();
            problem.setId(advertisement.getUrl());
            QuestionAndAnswerDetailActivity.start(context, problem, isFromPush);
        } else {
            gotoGeneralWebActivity(context, advertisement.getUrl(), isFromPush);
        }
    }

    /**
     * 跳转到详情
     *
     * @param context
     * @param info
     */
    public static void gotoDetailActivity(Context context, SelectInfo info) {
        if (AppKey.TYPE_ANSWER.equals(info.getDiscoverType())) {//回答
            Answer answer = new Answer();
            answer.setId(info.getId());
            answer.setTitle(info.getTitle());
            answer.setContent(info.getContent());
            answer.setUserMsg(info.getUserMsg());
            answer.setCommentCount(info.getCommentCount());
            answer.setAgreeCount(info.getAgreeCount());
            answer.setOpposeCount(info.getOpposeCount());
            answer.setCreateTime(info.getCreateTime());
            AnswerDetailActivity.start(context, answer, false);
        } else if (AppKey.TYPE_PROBLEM.equals(info.getDiscoverType())) {//问题
            Problem problem = new Problem();
            problem.setId(info.getId());
            problem.setTitle(info.getTitle());
            problem.setContent(info.getContent());
            problem.setImage(info.getImage());
            problem.setCommentCount(info.getCommentCount());
            problem.setAgreeCount(info.getAgreeCount());
            problem.setUserMsg(info.getUserMsg());
            problem.setCreateTime(info.getCreateTime());
            QuestionAndAnswerDetailActivity.start(context, problem, false);
        } else if (AppKey.TYPE_ARTICLE.equals(info.getDiscoverType())) {// 文章
            Article article = new Article();
            article.setId(info.getId());
            article.setTitle(info.getTitle());
            article.setContent(info.getContent());
            article.setImage(info.getImage());
            article.setCommentCount(info.getCommentCount());
            article.setAgreeCount(info.getAgreeCount());
            article.setUserMsg(info.getUserMsg());
            article.setCreateTime(info.getCreateTime());
            ArticleDetailActivity.start(context, article, false);
        } else if (AppKey.TYPE_DYNAMIC.equals(info.getDiscoverType())) {//动态
            Dynamic dynamic = new Dynamic();
            dynamic.setId(info.getId());
            dynamic.setTitle(info.getTitle());
            dynamic.setContent(info.getContent());
            dynamic.setImage(info.getImage());
            dynamic.setCommentCount(info.getCommentCount());
            dynamic.setAgreeCount(info.getAgreeCount());
            dynamic.setUserMsg(info.getUserMsg());
            dynamic.setCreateTime(info.getCreateTime());
            DynamicDetailActivity.start(context, dynamic, false);
        } else if (AppKey.TYPE_STORY.equals(info.getDiscoverType())) {
            Story story = new Story();
            story.setId(info.getId());
            story.setTitle(info.getTitle());
            story.setContent(info.getContent());
            story.setAudioUrl(info.getAudioUrl());
            story.setImage(info.getImage());
            story.setAgreeCount(info.getAgreeCount());
            story.setCommentCount(info.getCommentCount());
            story.setCreateTime(info.getCreateTime());
            story.setUserMsg(info.getUserMsg());
            ArrayList list = new ArrayList();
            list.add(story);
            StoryDetailActivity.start(context, list, 0, false);
        }
    }

    /**
     * 处理通知的跳转
     *
     * @param context
     * @param type
     * @param typeId
     */
    public static void processNotice(Context context, String type, String typeId, int openType, String url,boolean isFromPush) {
        if (0 == openType) {//官方通知页
            OfficialNoticeActivity.start(context,true);
        } else if (1 == openType) {//App首页
            gotoMainActivity(context);
        } else if (2 == openType) {//内容详情页面
            if (TextUtils.isEmpty(type) || TextUtils.isEmpty(typeId)) {
                return;
            }
            if (AppKey.TYPE_ANSWER.equals(type)) {//回答
                Answer answer = new Answer();
                answer.setId(typeId);
                AnswerDetailActivity.start(context, answer, isFromPush);
            } else if (AppKey.TYPE_PROBLEM.equals(type)) {//问题
                Problem problem = new Problem();
                problem.setId(typeId);
                QuestionAndAnswerDetailActivity.start(context, problem, isFromPush);
            } else if (AppKey.TYPE_ARTICLE.equals(type)) {// 文章
                Article article = new Article();
                article.setId(typeId);
                ArticleDetailActivity.start(context, article, isFromPush);
            } else if (AppKey.TYPE_STORY.equals(type)) {//故事
                Story story = new Story();
                story.setId(typeId);
                ArrayList list = new ArrayList();
                list.add(story);
                StoryDetailActivity.start(context, list, 0, isFromPush);
            }
        } else if (3 == openType) {//h5页面地址
            gotoGeneralWebActivity(context, url, isFromPush);
        }
    }

    /**
     * 播放故事
     * @param context
     * @param storyList
     * @param index
     */
    public static void playStory(Context context, List<Story> storyList, int index){
        Intent intent = new Intent(context, MusicService.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MusicService.MUSIC_INTENT_KEY, (Serializable) storyList);
        intent.putExtras(bundle);
        intent.putExtra(MusicService.INDEX, index);
        context.startService(intent);
    }

}
